<?php

// This report is used for specific metric graphs at the bottom of the
// cluster_view page.

/* Pass in by reference! */
function ganglia_graph_metric ( &$rrdtool_graph ) {

    global $ganglia_context,
           $ganglia_default_metric_color,
           $ganglia_hostname,
           $ganglia_jobstart,
           $ganglia_load_color,
           $ganglia_max,
           $ganglia_meta_designator,
           $ganglia_metricname,
           $ganglia_metrictitle,
           $ganglia_min,
           $ganglia_range,
           $ganglia_rrd_dir,
           $ganglia_size,
           $ganglia_summary,
           $ganglia_value,
           $ganglia_vlabel,
           $ganglia_strip_domainname;

    if ($ganglia_strip_domainname) {
        $ganglia_hostname = ganglia_strip_domainname($ganglia_hostname);
    }

    $rrdtool_graph['height'] += 0; //no fudge needed

    switch ($ganglia_context) {

        case 'host':

            if ($ganglia_summary) {
                $rrdtool_graph['title'] = $ganglia_hostname;
                $prefix = $ganglia_metricname;
            } else {
                $prefix = $ganglia_hostname;
                if ($ganglia_metrictitle) {
                   $rrdtool_graph['title'] = $ganglia_metrictitle;
                } else {
                   $rrdtool_graph['title'] = $ganglia_metricname;
                }
            }

            $prefix = $ganglia_summary ? $ganglia_metricname : $ganglia_hostname;
            $ganglia_value = ($ganglia_value > 1000)
                        ? number_format($ganglia_value)
                        : number_format($ganglia_value, 2);

            if ($ganglia_range == 'job') {
                $hrs = intval (-$jobrange / 3600);
                $subtitle = "$prefix last ${hrs} (now $ganglia_value)";
            } else {
                if ($ganglia_summary) {
                   $subtitle_one = "$ganglia_metricname last $ganglia_range";
                } else {
                   $subtitle_one = "$ganglia_hostname last $ganglia_range";
                }
                $subtitle_two = "  (now $ganglia_value)";
            }

            break;

        case 'meta':
            $rrdtool_graph['title'] = "$ganglia_meta_designator ". $rrdtool_graph['title'] ."last $ganglia_range";
            break;

        case 'grid':
            $rrdtool_graph['title'] = "$ganglia_meta_designator ". $rrdtool_graph['title'] ."last $ganglia_range";
            break;

        case 'cluster':
            $rrdtool_graph['title'] = "$clustername "    . $rrdtool_graph['title'] ."last $ganglia_range";
            break;

        default:
            if ($ganglia_size == 'small') {
                $rrdtool_graph['title'] = $ganglia_hostname;
            } else if ($ganglia_summary) {
                $rrdtool_graph['title'] = $ganglia_hostname;
            } else {
                $rrdtool_graph['title'] = $ganglia_metricname;
            }
            break;

    }

    if ($ganglia_load_color)
        $rrdtool_graph['color'] = "BACK#'$ganglia_load_color'";

    if (isset($ganglia_max) && is_numeric($ganglia_max))
        $rrdtool_graph['upper-limit'] = $ganglia_max;

    if (isset($ganglia_min) && is_numeric($ganglia_min))
        $rrdtool_graph['lower-limit'] = $ganglia_min;

    if ($ganglia_vlabel) {
        // We should set $ganglia_vlabel, even if it isn't used for spacing
        // and alignment reasons.  This is mostly for aesthetics
        $temp_vlabel = trim($ganglia_vlabel);
        $rrdtool_graph['vertical-label'] = strlen($temp_vlabel)
                   ?  $temp_vlabel
                   :  ' ';
    } else {
        $rrdtool_graph['vertical-label'] = ' ';
    }

    //# the actual graph...
    $series  = "DEF:'sum'='$ganglia_rrd_dir/$ganglia_metricname.rrd:sum':AVERAGE ";
    $series .= "AREA:'sum'#$ganglia_default_metric_color:'$subtitle_one'";
    $series .= ":STACK: COMMENT:'$subtitle_two'";

    if ($ganglia_jobstart) {
        $series .= "VRULE:$ganglia_jobstart#$jobstart_color ";
    }

    $rrdtool_graph['series'] = $series;

    return $rrdtool_graph;

}


